package com.jdyinfo.ic.controller;

import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jdyinfo.ic.mysql.entity.Building;
import com.jdyinfo.ic.service.BuildingService;
import com.jdyinfo.ic.util.IcUtil;

import io.swagger.annotations.Api;

@Api(value="/building",tags="dfds")
@RestController
@EnableAutoConfiguration
@RequestMapping("/")
public class BuildingController {
	Logger logger = LoggerFactory.getLogger(BuildingController.class);
	
	@Autowired
	ObjectMapper objectMapper;
	
	@Autowired
	BuildingService buildingService;
	
	@PostMapping(value = "/building", consumes = "application/json; charset=utf-8")
	int createUpDownPeriod(@RequestBody Building building) {
		Timestamp nowTimestamp = IcUtil.nowTimestamp();
		String createdBy = "-1";
		String lastUpdatedBy = "-1";
		
		building.setCreatedBy(createdBy);
		building.setCreatedDate(nowTimestamp);
		building.setLastUpdatedBy(lastUpdatedBy);
		building.setLastUpdatedDate(nowTimestamp);
		
		return buildingService.createBuilding(building);
	}
	
	@GetMapping(value = "/building")
	List<Building> getBuilding(@RequestParam("upTownPeriodId") int upTownPeriodId) {
		return buildingService.findBuilding(upTownPeriodId);
	}
	
	@PostMapping("/building/{id}")
	int changeBuilding(@PathVariable("id") int id, @RequestBody Building building) {
		String lastUpdatedBy = "-1";
		Timestamp lastUpdatedDate = IcUtil.nowTimestamp();
		
		building.setId(id);
		building.setLastUpdatedBy(lastUpdatedBy);
		building.setLastUpdatedDate(lastUpdatedDate);
		
		return buildingService.updateBuilding(building);
	}
	
	@DeleteMapping("/building/{id}")
	int deleteBuilding(@PathVariable("id") int id) {
		return buildingService.deleteBuilding(id);
	}
	
}
